package com.example.shop.base;

import com.example.shop.common.utils.Constants;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import java.io.Serializable;

/**
 * Ajax结果封装类
 * @author haocj
 * @param <T>
 */
public class Result<T> implements Serializable {

    private Integer code;
    private String msg;
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private T data;


    public Result() {
    }

    public Result(T data){
        this.data = data;
    }

    public Result(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Result(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    /**
     * 成功
     * @return
     */
    public static Result success(){
        Result result =new Result(Constants.SUCCESS,Constants.SUCCESS_MSG);
        return result;
    }

    /**
     * 自定义返回成功信息
     * @param msg
     * @return
     */
    public static Result success(String msg){
        Result result =new Result(Constants.SUCCESS,msg);
        return result;
    }

    /**
     * 返回成功数据
     * @param object
     * @return
     */
    public static Result success(Object object){
        Result result =new Result(Constants.SUCCESS,Constants.SUCCESS_MSG,object);
        return result;
    }

    public static Result success(String msg,Object object){
        Result result =new Result(Constants.SUCCESS,msg,object);
        return result;
    }

    public static Result error(){
        Result result = new Result(Constants.ERROR,Constants.ERROR_MSG);
        return result;
    }

    public static Result error(Integer code){
        Result result = new Result(code,Constants.ERROR_MSG);
        return result;
    }

    public static Result error(String msg){
        Result result = new Result(Constants.ERROR,msg);
        return result;
    }

    public static Result error(Integer code,String msg){
        Result result = new Result(code,msg);
        return result;
    }

}
